package com.sidianzhong.sdz.controller;


import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.model.commond.WebEquipment;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "二维码表")
@Controller
public class WebEquipmentscancodeController {

    @Autowired
    WebEquipmentscancodeService webEquipmentscancodeService;
    @Autowired
    WebEquipmentgroupService webEquipmentgroupService;
    @Autowired
    WebAequipmentgroupService webAequipmentgroupService;
    @Autowired
    WebHequipmentgroupService webHequipmentgroupService;
    @Autowired
    WebZequipmentgroupService webZequipmentgroupService;

    @Autowired
    HttpServletResponse response;



    @ApiOperation(value = "扫描获得设备")
    @RequestMapping(value = "/web_equipmentscancodes",
                    method = { RequestMethod.GET },
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebEquipmentscancodes(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "equipmentcode", required = false) String equipmentcode,
            @RequestParam(value = "equipmentcodetype", required = false) Integer equipmentcodetype
    ) {

        WebEquipmentscancode bycode = webEquipmentscancodeService.getBycode(equipmentcode, equipmentcodetype);
        if (bycode!=null){

        }
        WebEquipmentgroup bykey = webEquipmentgroupService.getBykey(bycode.getEquipmenttypekey());

        if (bykey.getTablename().equals("AEquipmentGroup")){

            WebAequipmentgroup byKey = webAequipmentgroupService.getByKey(bycode.getEquipmenttypekey(), bycode.getEquipmentkey());
            if (null ==byKey){
                return new ResponseEntity<>(ResultModel.ok(byKey), HttpStatus.OK);
            }
            WebEquipment webEquipment = new WebEquipment();
            webEquipment.setId(byKey.getId());
            webEquipment.setSitename(byKey.getSitename());
            webEquipment.setEquipmenttypekey(byKey.getEquipmenttypekey());
            webEquipment.setEquipmentname(byKey.getEquipmentname());
            webEquipment.setEquipmentkey(byKey.getEquipmentkey());
            webEquipment.setStatus(byKey.getStatus());
            return new ResponseEntity<>(ResultModel.ok(webEquipment), HttpStatus.OK);

        }
        if (bykey.getTablename().equals("HEquipmentGroup")){

            WebHequipmentgroup byKey = webHequipmentgroupService.getByKey(bycode.getEquipmenttypekey(), bycode.getEquipmentkey());
            if (null ==byKey){
                return new ResponseEntity<>(ResultModel.ok(byKey), HttpStatus.OK);
            }
            WebEquipment webEquipment = new WebEquipment();
            webEquipment.setId(byKey.getId());
            webEquipment.setSitename(byKey.getSitename());
            webEquipment.setEquipmenttypekey(byKey.getEquipmenttypekey());
            webEquipment.setEquipmentname(byKey.getEquipmentname());
            webEquipment.setEquipmentkey(byKey.getEquipmentkey());
            webEquipment.setStatus(byKey.getStatus());
            return new ResponseEntity<>(ResultModel.ok(webEquipment), HttpStatus.OK);
        }
        if (bykey.getTablename().equals("ZEquipmentGroup")){

            WebZequipmentgroup byKey = webZequipmentgroupService.getByKey(bycode.getEquipmenttypekey(), bycode.getEquipmentkey());
            if (null ==byKey){
                return new ResponseEntity<>(ResultModel.ok(byKey), HttpStatus.OK);
            }
            WebEquipment webEquipment = new WebEquipment();
            webEquipment.setId(byKey.getId());
            webEquipment.setSitename(byKey.getSitename());
            webEquipment.setEquipmenttypekey(byKey.getEquipmenttypekey());
            webEquipment.setEquipmentname(byKey.getEquipmentname());
            webEquipment.setEquipmentkey(byKey.getEquipmentkey());
            webEquipment.setStatus(byKey.getStatus());
            return new ResponseEntity<>(ResultModel.ok(webEquipment), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResultModel.ok(null), HttpStatus.OK);
    }
}
